<template>
  <div id="chooseTarget" v-if="show">
    <div class="banktitle">选择指标</div>
    <div class="bankBox" v-for="(item,index) in cities" :key="index">
      <div class="boxline">
        <span class="boxcity">{{item.type}}</span>
        <el-checkbox  v-model="item.check" @change="cityall(index, item)">全选该类别指标</el-checkbox>
      </div>
      <div class="yinhang">
        <span v-for="(ele, k) in item.list" :key="k" :class="[ele.checked? 'active':'','spanbank']" @click="spanClick(index, k)">{{ele.indicatorName}}</span>
      </div>
    </div>
    <div class="btnCh">
      <el-button type="primary" size="small" @click="allchoose">全选</el-button>
      <el-button type="primary" size="small" @click="noallchoose">全不选</el-button>
      <el-button type="primary" size="small" @click="wancheng">完成</el-button>
      <el-button type="primary" size="small" @click="cancel">取消</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'chooseTarget',
  data () {
    return {
      show: false,
      cities: [],
    };
  },
  mounted () {
    this.$bus.$on('chooseTarget', config => {
      if (!config) return (this.show = false);
      this.callback = config.callback;
      this.show = true;
    });
  },
  methods: {
    spanClick (index, k) {
      this.$set(this.cities[index].list[k], `checked`, !this.cities[index].list[k].checked);
      let boolgood = this.cities[index].list.every((element, i) => {
        return element.checked == true;
      });
      if (boolgood) {
        this.$set(this.cities[index], `check`, true);
      } else {
        this.$set(this.cities[index], `check`, false);
      };
    },
    // 获取接口数据渲染
    getCity() {
      this.axios.get('superviseIndicator/findAllType')
        .then(({data}) => {
          if (data.code == 200) {
            let city = data.data;
            city.forEach((item, index) =>{
              item.check = false;
            })
            this.cities = city;
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    noallchoose () {
      this.cities.forEach((item,index) =>{
        this.$set(this.cities[index], `check`, false);
        item.list.forEach((ele, i)=> {
          this.$set(this.cities[index].list[i], `checked`, false);
        });
      })
    },
    allchoose () {
      this.cities.forEach((item,index) =>{
        this.$set(this.cities[index], `check`, true);
        item.list.forEach((ele, i)=> {
          this.$set(this.cities[index].list[i], `checked`, true);
        });
      })
    },
    wancheng () {
      let status = false;
      this.cities.forEach((item,index) =>{
        item.list.forEach((ele, i)=> {
          // ele.checked = true;
          if (ele.checked) {
            status =true;
          }
        });
      })
      if (!status) {
        this.$message({message: '请您选择社行/机构',type: 'warning'});
        return false;
      }
      let banks=[];
      this.cities.forEach((item, index)=> {
        item.list.forEach((ele, i)=> {
          if (ele.checked) {
            banks.push(ele);
          }
        })
      })
      this.callback(banks);
      this.show= false;
    },
    cancel () {
      this.show= false;
    },
    // 单个城市的全选
    cityall (index, item) {
      if (item.check) {
        this.cities[index].list.forEach((element, i) => {
          this.$set(this.cities[index].list[i], `checked`, true);
        });
      } else {        
        this.cities[index].list.forEach((element, i) => {
          this.$set(this.cities[index].list[i], `checked`, false);
        });
      }
    }
  },
  watch: {
    // 每次都清空选中的值
    show () {
      if(this.show) {
        this.getCity();
      }
      if (this.cities.length != 0) {
        this.noallchoose();
      } 
    }
  }
};
</script>

<style lang="scss">
  #chooseTarget {
    position: absolute;
    left: 220px;
    right: 20px;
    top: 75px;
    bottom: 0px;
    z-index: 2000;
    padding-bottom: 20px;
    overflow-y: auto;
    background-color: rgb(225, 226, 220);
    .banktitle {
      height: 50px;
      line-height: 50px;
      font-size: 16px;
      padding-left: 20px;
      background-color:#809092;
      border-bottom: 1px solid #ccc;
      color: white;
    }
    .bankBox {
      .boxline{
        padding: 0 20px;
        display: flex;
        justify-content: space-between;
        height: 40px;
        align-items: center;
        background-color: #F0F0F0;
      }
    }
    .yinhang {
      background-color: white;
      padding: 20px;
      padding-top: 10px;  
      .spanbank {
        display: inline-block;
        padding: 5px 10px;
        border: 1px solid black;
        margin-right: 20px;
        margin-top: 10px;
        cursor: pointer;
        &.active {
          background-color: #3399CC;
          color: white;
        }
      }
    }
    .btnCh {
      margin-top: 20px;
      display: flex;
      justify-content: center;
      .el-button {
        margin-right: 20px;
      }
    }
  }
</style>
